package com.xunlai.infra.permission.business.service.impl;

import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.google.gson.Gson;
import com.xunlai.infra.permission.business.service.SmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liang
 * @date 9/13/23 7:02 AM
 */
@Service
public class SmsServiceImpl implements SmsService {

    private final Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);

    private AsyncClient client;

    @Override
    public void sendSms(String mobileNumber,String code) {
        //TODO
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(mobileNumber)
                .signName("北京逸客网")
                .templateCode("SMS_463594334")
                .templateParam("{\"code\":\"" + code +"\"}")
                .build();
        client.sendSms(sendSmsRequest).handleAsync((r,e) -> {
            String s = new Gson().toJson(r);
            logger.info(s);
            if(e != null){
                logger.error(e.getMessage(),e);
            }
            return r;
        });

    }

    @Autowired
    public void setClient(AsyncClient client) {
        this.client = client;
    }
}
